package topuprouter

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"nodeseller/RabbitMQ"
	"nodeseller/datamodels"
	"nodeseller/repositories"
	"nodeseller/response"
	"strconv"
	"time"
)
//创建缴费订单
func CreateTopUp(c *gin.Context)  {
	phone,err := c.Cookie("phone")
	if err != nil{
		response.Response1(c,1,"身份验证失败，请重新登陆","")
		return
	}
	cash := c.PostForm("cash")
	if cash == ""{
		response.Response1(c,1,"参数丢失,请稍后重试","")
		return
	}
	file,err := c.FormFile("file")
	if err != nil{
		response.Response1(c,1,"图片丢失，请重新上传","")
		return
	}
	path :=  strconv.FormatInt(time.Now().Unix(),10) + file.Filename
	err = c.SaveUploadedFile(file,"./public/images/" +path)
	if err != nil{
		response.Response1(c,1,"图片丢失，请重新上传","")
		return
	}
	var topupdata datamodels.TopUp
	c.ShouldBind(&topupdata)
	topupdata.Phonenumber = phone
	topupdata.Img = "/static/"+path
	topupdata.Timer = time.Now().Format("2006-01-02 15:04:05")
	rabbitmq := RabbitMQ.NewRabbitMQSimple("topup")
	data,_ := json.Marshal(topupdata)
	rabbitmq.PublishSimple(string(data))
	response.Response1(c,0,"缴费成功,请等待到账","")
}
//查询待到账的缴费订单
func FindAllWaitCancel(c *gin.Context)  {
	phone,err := c.Cookie("phone")
	if err != nil{
		response.Response1(c,1,"身份验证失败，请重新登陆","")
		return
	}
	result,err := repositories.NewTopUpManager().FindWaitCheck(phone)
	if err != nil{
		response.Response1(c,1,"数据错误，请稍后重试","")
		return
	}
	response.Response1(c,0,"查询成功",result)
}
//查询已到账的缴费订单
func FindAllFinedCancel(c *gin.Context)  {
	phone,err := c.Cookie("phone")
	if err != nil{
		response.Response1(c,1,"身份验证失败，请重新登陆","")
		return
	}
	result,err := repositories.NewTopUpManager().FindFinedCheck(phone)
	if err != nil{
		response.Response1(c,1,"数据错误，请稍后重试","")
		return
	}
	response.Response1(c,0,"查询成功",result)
}